package org.fkjava.oa.identity.dao.impl;

import java.util.List;

import org.fkjava.common.dao.impl.BaseDaoImpl;
import org.fkjava.oa.identity.dao.JobDao;
import org.fkjava.oa.identity.dao.ModuleDao;
import org.fkjava.oa.identity.dao.PopedomDao;
import org.fkjava.oa.identity.dao.RoleDao;
import org.fkjava.oa.identity.domain.Role;
import org.fkjava.oa.identity.domain.User;
import org.fkjava.oa.util.UserHolder;

/**
 * @author xlei
 * @Email dlei0006@163.com
 * @QQ 251425887
 * @Tel 18665616520
 * @Date 2017年1月14日上午9:44:47
 * @From http://www.fkjava.org 
 *
 */
public class PopedomDaoImpl extends BaseDaoImpl implements PopedomDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<String> selectRoleOpreasCodes(Role role, String parentCode) throws Exception {
		// 查询当前角色在当前模块下拥有的操作权限编号
		String hql = "select p.opera.code from Popedom p where p.role.id = ? and p.module.code =?";
		return (List<String>) find(hql, new Object[]{role.getId(),parentCode});
	}

	@Override
	public void clearRoleModuleOperas(Role role, String parentCode)
			throws Exception {
		String hql = "delete Popedom p where p.role.id = ? and p.module.code = ?";
		bulkUpdate(hql, new Object[]{role.getId(),parentCode});
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> getUserPopedomModuleCodes() throws Exception {
		/**查询当前用户的权限模块编号 ：先查用户所有的角色,再查这些角色拥有的所有权限模块  */
		StringBuilder hql = new StringBuilder();
		hql.append(" select distinct p.module.code from Popedom p ");
		hql.append(" where p.role.id in(");
		// 查询出当前用户的所有角色
		hql.append(" select r.id from Role r inner join r.users u ");
		hql.append(" where u.userId = ? ) order by p.module.code asc ");
		return (List<String>) find(hql.toString(), new Object[]{UserHolder.getCurrentUser().getUserId()} );
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> getUserPopedomOperasCodes() throws Exception {
		/** 查询当前用户的权限操作编号 ：先查用户所有的角色,再查这些角色拥有的所有权限操作编号 */
		StringBuilder hql = new StringBuilder();
		hql.append(" select distinct p.opera.code from Popedom p ");
		hql.append(" where p.role.id in(");
		// 查询出当前用户的所有角色
		hql.append(" select r.id from Role r inner join r.users u ");
		hql.append(" where u.userId = ? ) order by p.opera.code asc ");
		return (List<String>) find(hql.toString(), new Object[]{UserHolder.getCurrentUser().getUserId()} );
	}
	
}





